package Test;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        System.out.println("运行开始");
        int length = 100;//数组长度的范围
        int N = 1000;//数组内数字大小的范围
        int time = 50000;//运行次数

        for (int i = 0; i < time; i++) {
            int ranLength = (int)(Math.random()*length);
            int[] arr = new int[ranLength];
            arr = createArr(arr, N);
            Arrays.sort(arr);
            int rand = (int)(Math.random()*N)+1;
            if(rightAns(arr, rand)!=leftFind(arr, rand)) {
                /*
                 * System.out.println(rightAns(arr, rand)); System.out.println(leftFind(arr,
                 * rand));
                 */
                System.out.println("出错了");
            }
        }
        System.out.println("运行结束");
    }

    public static int[] createArr(int[] arr, int n) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*n)+1;
        }
        return arr;
    }

    public static int rightAns(int[] arr, int n) {
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]>=n) {
                return i;
            }
        }
        return -1;
    }

    public static int leftFind(int[] arr, int n) {
        int l = 0,r = arr.length-1,m = 0,index = -1;
        while(l<=r) {
            m = (r+l)/2;
            if(arr[m]>=n) {
                index = m;
                r = m-1;
            } else {
                l = m+1;
            }
        }
        return index;
    }
}

class Solution {
    public int findPeakElement(int[] nums) {
        if(nums.length==1) {
            return 0;
        }
        int n = nums.length;
        if(nums[0]>nums[1]) {
            return 0;
        }
        if(nums[n-1]>nums[n-2]) {
            return n-1;
        }
        int l = 1,r = n-2,m = 0;
        while(l<=r) {
            m = (r+l)/2;
            if(nums[m]<nums[m-1]) {
                r = m-1;
            } else if(nums[m]<nums[m+1]) {
                l = m+1;
            } else {
                break;
            }
        }
        return m;
    }
}